package com.xiaoyu.lanling.feature.invite.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.invite.list.InviteListEvent
import com.xiaoyu.lanling.feature.invite.model.InviteItem

class InviteListData : PagedListDataModelWithRequestTag<InviteItem, Any>() {
    val tag = Any()

    override fun doQueryData() {
        RequestCreator.create(InviteListEvent::class.java)
                .appendQueryDataTo(listPageInfo)
                .setRequestUrl(HttpConfig.LANLING_INVITE_INVITE_LIST)
                .setRequestHandler(object : RequestDefaultHandler<InviteListEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): InviteListEvent {
                        val rawData = originData.optJson("data")
                        val rawList = rawData.optJson("list")
                        val list = ListUtil.convertToList(rawList.toList()) {
                            InviteItem(it)
                        }
                        updateResult(rawData, list)
                        return InviteListEvent(requestTag)
                    }

                    override fun onRequestFinish(event: InviteListEvent?) {
                        event?.post()
                    }
                })
                .enqueue()
    }

    override fun getRequestTag(): Any {
        return tag
    }
}